package com.cogent.dynamicform.form.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.cogent.business.helper.QuerySelectHelper;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;

public class DefQuerySelectDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		String curvalue = MapUtil.getStringValue(formMap, formColumnDomain.getName()); 
		String sql = formColumnDomain.getTypecode();
		List list = BussinessQueryHelper.query(sql);
		StringBuffer str = new StringBuffer();
		str.append("<select name=\"").append(formColumnDomain.getName()).append("\" id=\"").append(formColumnDomain.getName()).append("\"/>");
		str.append("<option value=\"\">请选择</option>");
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap dataMap = (HashMap)it.next();
			str.append("<option value=\"").append(MapUtil.getStringValue(dataMap, "enumvalue")).append("\"");
			if(curvalue.equals(MapUtil.getStringValue(dataMap, "enumvalue")))
				str.append(" selected=\"selected\"");
			str.append(">").append(MapUtil.getStringValue(dataMap, "enumoption")).append("</option>");
		}
		str.append("</select>");
		
		return str.toString();
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap) + "&nbsp;";
		return QuerySelectHelper.genSelectField(formColumnDomain.getTypecode(), formColumnDomain.getName(), formMap, formColumnDomain.getDefaultvalue(),
				formColumnDomain.getShowcontrol(), formColumnDomain.getShowclass(), formColumnDomain.isIsrequired(), true);
	}
	
	@SuppressWarnings("unchecked")
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getShowtype() == 1)
			return "&nbsp;";
		String ret = QuerySelectHelper.getShowValue(formColumnDomain.getTypecode(), formColumnDomain.getName(), formMap);
		if(StringUtil.isEmpty(ret))
			ret = "&nbsp;";
		return ret;
	}
}